import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:ball_lion/module/live/live_statstics_logic.dart';
import 'package:ball_lion/entity/live/live_statistics_entity.dart';
import 'package:ball_lion/resources/styles.dart';
import 'package:ball_lion/utils/utils.dart';
import 'package:ball_lion/widgets/circular.dart';
import 'package:ball_lion/widgets/loading/loading_widget.dart';
import 'package:ball_lion/widgets/base/wqs_text.dart';

import 'package:ball_lion/resources/colours.dart';

class LiveStatisticsPage extends StatefulWidget {
  const LiveStatisticsPage({super.key});

  @override
  State<LiveStatisticsPage> createState() => _LiveStatisticsPageState();
}

class _LiveStatisticsPageState extends State<LiveStatisticsPage> {
  final LiveStatisticsLogic logic = Get.put(LiveStatisticsLogic());
  @override
  Widget build(BuildContext context) {
    return GetBuilder<LiveStatisticsLogic>(builder: (logic) {
      return Scaffold(
        backgroundColor: const Color(0xFF141618),
        appBar: Styles.appBar(
            backgroundColor: Colours.transparent,
            leadingColor: Colours.white,
            systemUiOverlayStyle: const SystemUiOverlayStyle(
                statusBarBrightness: Brightness.light,
                statusBarIconBrightness: Brightness.light)),
        body: LoadingWidget<bool>(
          isLoading: logic.statistics == null,
          loading: Container(),
          data: logic.statistics?.data.noValue ?? true,
          noData: Stack(
            children: [
              Column(
                children: [
                  Container(height: 24),
                  const QsText("直播结束", fontSize: 24, color: Colours.white),
                ],
              ),
              Center(
                child: QsText(
                  "直播暂无数据",
                  color: Colours.white.withOpacity(0.5),
                  fontSize: 16,
                  textAlign: TextAlign.center,
                ),
              )
            ],
          ),
          child: () => SingleChildScrollView(
            child: Column(
              children: [
                Container(height: 24),
                const QsText("直播结束", fontSize: 24, color: Colours.white),
                if (logic.statistics?.liveTime.hasValue ?? false)
                  QsText("直播时长 ${logic.statistics?.liveTime}",
                          fontSize: 16, color: Colours.white.withOpacity(0.5))
                      .paddingOnly(top: 8),
                Container(height: 26),
                ...logic.statistics?.data
                        ?.map((e) => statisticsDetail(e))
                        .toList() ??
                    [],
                Container(height: 12),
                QsText(
                  "当用户量比较大时，直播数据生成可能需要较多时间\n请稍后通过直播列表，查看直播数据",
                  color: Colours.white.withOpacity(0.5),
                  fontSize: 12,
                  textAlign: TextAlign.center,
                ),
                Container(height: 50)
              ],
            ),
          ),
        ),
      );
    });
  }

  Widget statisticsDetail(StatisticsDetail entity) {
    return entity.content.noValue
        ? Container()
        : Circular(
            radius: 8,
            margin: const EdgeInsets.fromLTRB(12, 0, 12, 12),
            padding: const EdgeInsets.all(12),
            color: Colours.white.withOpacity(0.05),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                QsText(entity.title ?? "",
                        color: Colours.white.withOpacity(0.5))
                    .paddingOnly(left: 4),
                Container(height: 12),
                GridView.count(
                    physics: const NeverScrollableScrollPhysics(),
                    shrinkWrap: true,
                    crossAxisCount: 3,
                    crossAxisSpacing: 8,
                    mainAxisSpacing: 8,
                    childAspectRatio: 104 / 80,
                    children:
                        entity.content?.map((e) => dataItem(e)).toList() ?? []),
              ],
            ),
          );
  }

  Widget dataItem(StatisticsData data) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        QsText(data.value ?? "-",
            fontSize: data.value?.contains("生成中") ?? false ? 14 : 16,
            color: Colours.white,
            fontWeight: FontWeight.w500),
        Container(height: data.value?.contains("生成中") ?? false ? 6 : 4),
        QsText(data.name ?? "",
            fontSize: 12, color: Colours.white.withOpacity(0.5))
      ],
    );
  }
}
